/// @author  Alberto Franco
/// @file    receiver.go
/// @date    09-05-2011
/// @brief   Contains the definition of the receiver process.  
package netsim

import (
    "fmt"
)

/// Receiver type receives packets, send back acks and deliver pkts to 
/// application layer.
type Receiver struct {
    name      string       ///< Name of the receiver
    output    chan int32   ///< Application layer connection
    ackChan   chan int32   ///< Acks channel
    transChan *chan int32  ///< Connection to the medium
}

/// Create a new data structure with the given name
func CreateReceiver(name string) *Receiver{
    var receiver *Receiver = new (Receiver)
    
    receiver.name       = name
    receiver.ackChan    = make (chan int32)
    receiver.output     = make (chan int32)
    
    return receiver
}

/// Receive the data from the medium and delivers it. CCS definition
/// Receiver = trans(x).Del(x)
/// Del(x)   = 'output(x).'ack.Receiver
func (self *Receiver) ReceiveData() {
    var packet int32
    for {
        /// Receive, deliver and ack
        packet = <- (*self.transChan)
        fmt.Println("Received pkt!")
        self.output <- packet
        self.ackChan <- 1
    }
}